package client.ui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.ScrollPaneConstants;

import client.RenrenClient;
import entity.UserInfo;

public class FriendsPane extends JTabbedPane {

	private static final long serialVersionUID = -8465032897975308139L;

	JPanel myfriends_panel;
	JPanel reffriends_panel;

	JPanel friends_list;
	JFormattedTextField inputField;
	JButton searchButton;
	JScrollPane jsp;

	long UID;
	String name;

	public FriendsPane(int _tabPlacement, int _tabLayoutPolicy, long _uid,
			String _name) {
		super(_tabPlacement, _tabLayoutPolicy);
		UID = _uid;
		name = _name;
		this.setBounds(2, 0, MainWindow.WIDTH, MainWindow.HEIGHT);
		this.setVisible(true);

		myfriends_panel = new JPanel();
		myfriends_panel.setBounds(0, 0, MainWindow.WIDTH - 50,
				MainWindow.HEIGHT);
		myfriends_panel.setLayout(null);
		reffriends_panel = new JPanel();
		reffriends_panel.setBounds(0, 0, MainWindow.WIDTH - 50,
				MainWindow.HEIGHT);
		reffriends_panel.setLayout(null);

		friends_list = new JPanel();
		friends_list.setLayout(null);

		inputField = new JFormattedTextField();
		inputField.setBounds(10, 10, 100, 25);
		inputField.addActionListener(new SearchButtonListener());
		myfriends_panel.add(inputField);

		searchButton = new JButton("查找");
		searchButton.setBounds(120, 10, 60, 25);
		searchButton.addActionListener(new SearchButtonListener());
		myfriends_panel.add(searchButton);

		jsp = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

		this.addTab("好友", myfriends_panel);
		this.addTab("推荐好友", reffriends_panel);

		this.setSelectedIndex(0);
	}

	class SearchButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String nickname = inputField.getText();
			searchUser(nickname);
		}
	}

	private void searchUser(String _nickname) {

		friends_list.removeAll();
		friends_list.grabFocus();

		Vector<UserInfo> vector = new Vector<UserInfo>();
		vector = RenrenClient.client.searchUser(UID, _nickname);

		for (int i = 0; i < vector.size(); i++) {
			final UserInfo tmpInfo = vector.elementAt(i);

			JLabel userinfo = new JLabel(tmpInfo.getNickname());
			userinfo.setBounds(5, i * 60, 80, 25);
			userinfo.setForeground(MainWindow.NAME_COLOR);
			userinfo.setFont(new Font("楷体_GB2312", Font.ITALIC, 15));
			userinfo.setVisible(true);
			friends_list.add(userinfo);

			String schoolStr = (tmpInfo.getSchool().equals("")) ? "无" : tmpInfo
					.getSchool();
			JLabel schoolinfo = new JLabel("学校: " + schoolStr);
			schoolinfo.setBounds(10, i * 60 + 27, 120, 25);
			schoolinfo.setForeground(MainWindow.NAME_COLOR);
			schoolinfo.setFont(new Font("微软雅黑", Font.BOLD, 13));
			schoolinfo.setVisible(true);
			friends_list.add(schoolinfo);

			if (!tmpInfo.getName().equals(name)) {
				JButton addButton = new JButton("添加");
				addButton.setBounds(90, i * 60, 60, 25);
				addButton.setVisible(true);
				addButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						if (RenrenClient.client.addFriend(UID, tmpInfo.getUID())) {
							JOptionPane
									.showMessageDialog(null, "添加成功",
											"提示信息",
											JOptionPane.INFORMATION_MESSAGE);
						}
					}
				});
				friends_list.add(addButton);
			}
			friends_list.setPreferredSize(new Dimension(200, i * 60));
		}
		friends_list.setVisible(true);

		jsp.getViewport().add(friends_list);
		jsp.setBounds(12, 50, 173, 230);
		jsp.setVisible(true);
		myfriends_panel.add(jsp);
		friends_list.repaint();
		jsp.repaint();
		myfriends_panel.repaint();
	}

	public void refresh() {
		friends_list.removeAll();
		friends_list.repaint();
		inputField.setText(null);
	}
}
